<template>
  <div>
    <div class="filter-more">
      <div class="from_header">
        <el-form ref="form" label-width="120px" inline>
          <el-form-item label="角色名称：">
            <el-input
              v-model.trim="roleQuery.roleName"
              clearable
              size="small"
              placeholder="请输入客户"
              @clear="selected"
            />
          </el-form-item>
        </el-form>

        <div class="establish">
          <el-button
            type="primary"
            size="small"
            @click="selected"
          >查询</el-button
          >
          <el-button type="primary" size="small" @click="add">新增</el-button>
        </div>
      </div>
    </div>

    <el-table
      :data="roleList"
      :header-row-style="{
        'background-color': '#E6E6E6',
        'font-weight': 'bold',
        color: '#333'
      }"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      height="650"
      align="center"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
    >
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="id"
        label="编号"
      />
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="roleName"
        label="角色名称"
      />
      <el-table-column fixed="right" align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="() => authority(scope.row)"
          >权限配置
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="() => upload(scope.row)"
          >修改
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="() => deleteRole(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="roleQuery.page"
      :total="total"
      :page-size="roleQuery.limit"
      style="padding: 30px 0; text-align: end"
      layout="total, prev, pager, next,jumper"
      @current-change="fetchData"
    />

    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="dialogFormValue"
      @close="closeDialog"
    >
      <el-form
        ref="role"
        :model="role"
        :rules="validateRole"
        label-width="120px"
      >
        <!-- <el-form-item v-if="isUpdate" label="角色编号：" prop="roleId">
          <el-col :span="8">
            <el-input
              v-model="role.roleId"
              disabled
              size="small"
              placeholder="请输入角色编号"
            />
          </el-col>
        </el-form-item> -->

        <el-form-item label="角色名称：" prop="roleName">
          <el-col :span="8">
            <el-input
              v-model="role.roleName"
              size="small"
              placeholder="请输入角色名称"
            />
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="restData()">取 消</el-button>
        <el-button type="primary" @click="append('role')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialog" title="配置权限">
      <div>
        <el-tree
          ref="treed"
          :data="authorityData"
          :default-checked-keys="keyList"
          show-checkbox
          node-key="id"
        />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="update()">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import ViewSessions from './ViewSessions.vue'
import user from '../../api/acl/user.js'
export default {
  components: {
    ViewSessions
  },
  data() {
    return {
      keyList: [],
      dialog: false,
      dataList: [],
      authorityData: [
        {
          id: 1,
          label: '订单管理'
        },
        {
          id: 2,
          label: '产品管理'
        },
        {
          id: 3,
          label: '派单'
        },
        {
          id: 4,
          label: '请购清单'
        },
        {
          id: 5,
          label: '订单费用'
        },
        {
          id: 6,
          label: '人员管理'
        },
        {
          id: 7,
          label: '角色管理'
        }
      ],
      isUpdate: false,
      dialogFormVisible: false,
      dialogFormValue: '新增角色',
      roleList: [],
      total: 0, // 数据库中的总记录数
      validateRole: {
        roleId: [
          { required: true, trigger: 'change', message: '请输入角色编号' }
        ],
        roleName: [
          { required: true, trigger: 'change', message: '请输入角色名称' }
        ]
      },
      roleIdUpdate: '',
      role: {
        roleId: '',
        roleName: ''
      },
      roleQuery: {
        roleName: '',
        page: 1,
        limit: 10
      }
    }
  },
  mounted() {
    this.roleListClick()
  },
  methods: {
    update() {
      const menuStr = this.$refs.treed.getCheckedKeys()
      const menuNameStr = this.$refs.treed
        .getCheckedNodes()
        .map(el => el.label)
        .toString()
      console.log(menuStr, menuNameStr)

      // if (menuNameStr.indexOf('工程工艺管理') != -1 || menuNameStr.indexOf('项目总清单') != -1 || menuNameStr.indexOf('采购申请') != -1) {
      //   if(menuNameStr.indexOf('生产管理') == -1) {
      //     menuNameStr = menuNameStr + ',生产管理'
      //   }
      // }
      // if (menuNameStr.indexOf('仓库列表') != -1 || menuNameStr.indexOf('出入库记录') != -1) {
      //   if(menuNameStr.indexOf('仓库管理') == -1) {
      //     menuNameStr = menuNameStr + ',仓库管理'
      //   }
      // }
      // if (menuNameStr.indexOf('采购列表') != -1 || menuNameStr.indexOf('采购记录') != -1) {
      //   if(menuNameStr.indexOf('采购管理') == -1) {
      //     menuNameStr = menuNameStr + ',采购管理'
      //   }
      // }
      // if (menuNameStr.indexOf('原材料品检管理') != -1 || menuNameStr.indexOf('产品品检管理') != -1) {
      //   if(menuNameStr.indexOf('品检管理') == -1) {
      //     menuNameStr = menuNameStr + ',品检管理'
      //   }
      // }
      // if (menuNameStr.indexOf('人员管理') != -1 || menuNameStr.indexOf('角色管理') != -1 || menuNameStr.indexOf('供应商管理') != -1 || menuNameStr.indexOf('客户管理') != -1 || menuNameStr.indexOf('系统管理') != -1) {
      //   if(menuNameStr.indexOf('权限管理') == -1) {
      //     menuNameStr = menuNameStr + ',权限管理'
      //   }
      // }

      console.log(menuStr, menuNameStr)
      user
        .rolesAuthorityUpdate({
          roleId: this.roleIdUpdate,
          name: menuNameStr
        })
        .then(response => {
          console.log(response);
          this.dialog = false;
          this.$message({
            message: "修改成功",
            duration: 1000
          });
        });
    },
    rolesAuthorityList(val) {
      this.keyList = []
      user
        .rolesAuthorityList({
          roleId: val
        })
        .then(response => {
          console.log(response)
          for (let i = 0; i < response.length; i++) {
            response[i].label = response[i].name
            response[i].children = response[i].child
            if (response[i].check) {
              this.keyList.push(Number(response[i].id))
            }
            for (let j = 0; j < response[i].children.length; j++) {
              response[i].children[j].label = response[i].children[j].name
              if (response[i].children[j].check) {
                this.keyList.push(Number(response[i].children[j].id))
              }
            }
          }
          this.authorityData = response
          this.$nextTick(function() {
            this.$refs.treed.setCheckedKeys(this.keyList)
          })
          console.log(this.keyList)
        })
    },
    authority(val) {
      this.dialog = true
      this.roleIdUpdate = val.id
      this.rolesAuthorityList(val.id)
    },
    restData() {
      this.dialogFormVisible = false
      this.dialogFormValue = ''
    },
    append(val) {
      this.$refs[val].validate(valid => {
        if (valid) {
          if (this.isUpdate) {
            user
              .roleUpdate({
                id: this.role.roleId,
                roleName: this.role.roleName
              })
              .then(response => {
                this.$message({
                  message: '修改成功',
                  duration: 1000
                })
                this.dialogFormVisible = false
                this.roleListClick()
              })
          } else {
            user
              .roleAdd({
                roleName: this.role.roleName
              })
              .then(response => {
                this.$message({
                  message: '新增成功',
                  duration: 1000
                })
                this.dialogFormVisible = false
                this.roleListClick()
              })
          }
        }
      })
    },
    selected() {
      this.roleQuery.page = 1
      this.roleListClick()
    },
    fetchData(val) {
      this.roleQuery.page = val
      this.roleListClick()
    },
    roleListClick() {
      user.roleList(this.roleQuery).then(response => {
        this.total = response.total
        this.roleList = response.list
      })
    },
    deleteRole(val) {
      if (!val.id) return false
      this.$confirm('是否删除该角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          user.roleDelete({ id: val.id }).then(res => {
            this.$message({
              message: '删除成功',
              duration: 1000
            })
            this.roleListClick()
          })
        })
        .catch(() => {})
    },
    closeDialog() {
      this.role = {
        roleId: '',
        roleName: ''
      }
      this.$refs.role.resetFields()
    },
    add() {
      this.isUpdate = false
      this.dialogFormVisible = true
      this.dialogFormValue = '新增角色'
    },
    upload(val) {
      this.isUpdate = true
      this.dialogFormVisible = true
      this.dialogFormValue = '修改角色'
      this.role = {
        roleId: val.id,
        roleName: val.roleName
      }
    }
  }
}
</script>
<style scoped>
.establish {
  padding-right: 20px;
  text-align: right;
}
</style>
